<script lang="ts">
export default {
  name: "GoodsList",
  filters:{
    date(v){
      const time = new Date(v);
      return time.getFullYear()+"-"+
          (time.getMonth()+1).toString().padStart(2,0)+"-"+
          time.getDate().toString().padStart(2,0)+" "+
          time.getHours().toString().padStart(2,0)+":"+
          time.getMinutes().toString().padStart(2,0)+":"+
          time.getSeconds().toString().padStart(2,0);
    }
  },
  methods:{
    addToCart(item){
      if(!localStorage.getItem('loginInfo')){
        this.$router.push('/login');
      }else {
        this.$store.commit('ADD_TO_MY_CART',item)
      }
    }
  }
}
</script>

<template>
  <div>
    <ul v-for="item in $store.state.goodsList" :key="item.id">
      <li>商品名称 : {{item.productName}}</li>
      <li>商品价格 : {{item.productPrice}} 元</li>
      <li>商品库存 : {{item.productStock}}</li>
      <li>上架时间 : {{item.addTime | date}}</li>
      <button @click="addToCart(item)">添加购物车</button>
      <hr/>
    </ul>
  </div>
</template>

<style scoped>

</style>
